<?php
namespace app\api\controller\v1;
use app\common\controller\Api;
use app\common\service\UserBank  as UserBankService;
use app\common\service\UserBank;
use app\common\service\UserDeposit as UserDepositService;
use app\common\service\UserDeposit;
use app\common\service\UserIntegral as UserIntegralService;
use \app\common\service\UserMoneyLog as UserMoneyLogService;
use think\Db;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/23
 * Time: 14:55
 */
class UserMoneyLog extends Base {
    protected $noNeedLogin = ['MyMoney'];
    protected $noNeedRight = '*';

    /**
     * 我的余额
     */
    public function MyMoney(){
        $userModel = new \app\common\model\User();
        $userinfo = $this->auth->getUser();
        $uid = $userinfo['id'];
        //我的余额
        $data['money'] = $userModel->getMyIntegral($uid,2);
        $data['can_carry_money']=$data['money'];
        if(!empty($data['can_carry_money'])){
            $data['can_carry_money']=(float)round($data['can_carry_money'],2);
        }else{
            $data['can_carry_money']=0;
        }
        $UserMoneyLogService = new UserMoneyLogService();
        $map['user_id']=$uid;
        $map['money_type']=array('in',array('1','2','3','4','7','5','9','10','11','13'));
        $order='';
        $offset = $this->request->request('offset');
        $offsets = $offset?$offset:1;
        $number = $this->request->request('limit');
        $limit = $number?$number:10;
//        $limits = $offsets.','.$limit;
//        $data['list'] = $UserMoneyLogService->getList($map,$order,$limits);
        $UserMoneyLogService = new UserMoneyLogService();
        $result= $UserMoneyLogService->getmyPonit($map,$limit,$offsets);
        $data['list'] = $result['data']['list'];

        if(!$data['list']){
            return json(array('code'=>0,"data"=>$data,'msg'=>"暂无余额记录"));
        }
        $this->success("请求成功",$data);
    }

    /**
     * 我要提现  数据处理
     */
    public function getDeposit(){
        $post = $this->request->request('rows');


        if(!$post){
            $datas = json_encode((object)null);
            $this->error("传参格式有误",$datas,0);
        }
        $userinfo = $this->userinfo;
        $uid = $userinfo['id'];
        $data = json_decode($post,true);
        $site = $this->site;
        $user = $this->auth->getUser();
        $UserDepositService = new UserDepositService();
        $res = $UserDepositService->setUserDeposit($site,$data,$user,$uid);
        if(!$res){
            $this->error($res['msg'],$res['data'],$res['code']);
        }
        $this->success($res['msg'],$res['data'],$res['code']);
    }
    /**
     * 我要提现
     */
    public function MyDeposit(){
        $post = $this->request->request('rows');
        if(!$post){
            $datas = json_encode((object)null);
            $this->error("传参格式有误",$datas,0);
        }
        $data = json_decode($post,true);
        $UserMoneyLogService = new UserMoneyLogService();
        $res = $UserMoneyLogService->getDeposit($data);
        if(!$res){
            $this->error($res['msg'],$res['data'],$res['code']);
        }
        $this->success($res['msg'],$res['data'],$res['code']);
    }

    /**
     * 提款金额银行卡数据
     */
    public function MyDepositData(){
        $UserBankService = new UserBankService();
        $userinfo = $this->userinfo;
        $map['user_id']= $userinfo['id'];
        $site = $this->site;
        $order = "createtime desc";
        $offset = $this->request->request('offset');
        $offsets = $offset?$offset:0;
        $number = $this->request->request('limit');
        $limit = $number?$number:6;
        $limits = $offsets.','.$limit;
        $field = "id,account,banking_id";
        $result = $UserBankService->select($map,$field,$order,$limits);
        foreach($result as &$v){

            $v['account'] = formatBankCardNo($v['account']);
            $v['bankname']=getBrankName($v['banking_id']);
            $v['banktype'] = "储蓄卡";
        }
        $result['cash_min'] = $site['cash_min'];
        $result['cash_beishu'] = 0;
        if($result){
            $this->success('success',$result);
        }else{
            $data = $this->nullcode;
            $this->error('error',$data);
        }
    }

    /**
     * 提现明细
     * @return \think\response\Json
     */
    public function AccountDetail(){
        $post = $this->request->request('rows');

        if(!$post){
            $datas = json_encode((object)null);

            $this->error("传参格式有误",$datas,0);
        }
        $data = json_decode($post,true);
        $userinfo =$this->userinfo;
        $where['user_id']= $userinfo['id'] ;
//        $where['status']=array('in',array('1','2'));
        //日期或者是开始时间
        if(isset($data['date'])){
            $date=$data['date'];
        }else{
            $date=0;
        }

        //结束时间
        if(isset($data['endDate'])){
            $endDate=$data['endDate'];
        }else{
            $endDate=0;
        }

        //周
        if(isset($data['week'])){
            $week=$data['week'];
        }else{
            $week=0;
        }

        //月
        if(isset($data['monthday'])){
            $monthDay=$data['monthday'];
        }else{
            $monthDay=0;
        }

        //以前月
        if(isset($data['month'] )){
            $month=$data['month'];
        }else{
            $month=0;
        }

        //年
        if(isset($data['year'] )){
            $year=$data['year'];
        }else{
            $year=0;
        }

        //判断如传递年为空就默认当前年
        if(empty($year)){
            $year=date("Y", time()); //年
        }

        //获取指定周开始时间和结束时间
        if(!empty($week)&&!empty($year)){
            $weekArray=weekday($year,$week);
        }else{
            $weekArray=[];
        }

        //获取指定月份开始时间戳和结束时间戳
        if(!empty($year)&&!empty($monthDay)){
            $monthArray=monthday($year,$monthDay);
        }else{
            $monthArray=[];
        }
        if(!empty($date)&empty($endDate)){
            //查询日期
            $dateime=strtotime($date);//1*24*60*60
            $where['createtime']=array('between',array($dateime,($dateime+1*24*60*60-1)));//array('eq',strtotime($date));
        }elseif(!empty($date)&!empty($endDate)){
            //查询带有开始时间和结束时间
            $dateime=strtotime($date);
            $endDate=strtotime($endDate);
            $where['createtime']=array('between',array($dateime,$endDate));
        }elseif(!empty($month)){
            //查询当前月份
            if(empty($monthDay)){
                $monthDay=date('m');
            }
            $monthArray=monthday($year,$monthDay);
            $where['createtime']=array('between',array($monthArray['start'],$monthArray['end']));
        }elseif(!empty($weekArray)){
            //查询指定周
            $where['createtime']=array('between',array($weekArray['start'],$weekArray['end']));
        }elseif(!empty($monthArray)){
            //查询指定月
            $where['createtime']=array('between',array($monthArray['start'],$monthArray['end']));
        }else{
            if(empty($monthDay)){
                $monthDay=date('m');
            }
            $monthArray=monthday($year,$monthDay);
            $where['createtime']=array('between',array($monthArray['start'],$monthArray['end']));

        }

        $userModel = new \app\common\model\User();
        $uid = $userinfo['id'];
        //我的余额
        $result['money'] =$userinfo['money'];


        $offset = $this->request->request('offset');
        $offsets = $offset?$offset:1;
        $number = $this->request->request('limit');
        $limit = $number?$number:10;
        $UserDeposit =  new UserDeposit();

        $list=$UserDeposit->getUserDeposit($where,$limit,$offsets);
        if(empty($list)){
            $data['total']=0;
            $data['list']=[];
            return json(array('code'=>0,"data"=>$data,'msg'=>"暂无数据"));
        }
        foreach ($list as $key=>$val){
            if($val['status']==1){
                $list[$key]['compellation']='审核中';
            }elseif($val['status']==2){
                $list[$key]['compellation']='审核通过';
            }elseif($val['status']==3){
                $list[$key]['compellation']='审核失败';
            }
        }
        $result['list'] = $list;
        $this->success('请求成功',$result,1);
    }

    /**
     * 查看余额 提现明细
     */
    public function getMyYue(){
        $site = $this->site;
        $user=$this->auth->getUser();
        $mentioning=$user['money'];
        $this->success('请求成功',$mentioning,1);
    }
    /**
     * 我的分红 1   我的贡献值 2
     */
    public function myPonit(){
        $userinfo = $this->userinfo;
        $type = $this->request->request('type');
        switch ($type)
        {
            case 1:
                $where['money_type']=array('eq','4');
              break;
            case 2:
                $where['money_type']=array('eq','8');
                  break;
            default:
        }
        $uid = $userinfo['id'];
        $where['user_id']=$uid;
        $offset = $this->request->request('offset');
        $offsets = $offset?$offset:1;
        $number = $this->request->request('limit');
        $limit = $number?$number:10;
//        $limits = $offsets.','.$limit;
        $UserMoneyLogService = new UserMoneyLogService();
        $res = $UserMoneyLogService->getmyPonit($where,$limit,$offsets);

        if(!$res){

            $this->error($res['msg'],$res['data'],$res['code']);
        }

        $this->success($res['msg'],$res['data'],$res['code']);
    }


    /**
     * 我的贡献值
     */
    public function myContribution(){
        $userinfo = $this->userinfo;
        $uid = $userinfo['id'];
        $where['user_id']=$uid;
        $where['money_type']=array('eq','8');
        $offset = $this->request->request('offset');
        $offsets = $offset?$offset:1;
        $number = $this->request->request('limit');
        $limit = $number?$number:10;
        $UserMoneyLogService = new UserMoneyLogService();
        $res = $UserMoneyLogService->getmyPonit($where,$limit,$offsets);
        if(!$res){
            $this->error($res['msg'],$res['data'],$res['code']);
        }
        $this->success($res['msg'],$res['data'],$res['code']);
    }
    /**
     * 我的积分
     */
    public function myUserIntegral(){
        $UserIntegralService =  new UserIntegralService();
        $userinfo = $this->userinfo;
        $uid = $userinfo['id'];
        $where['user_id']=$uid;
        $offset = $this->request->request('offset');
        $offsets = $offset?$offset:1;
        $number = $this->request->request('limit');
        $limit = $number?$number:10;
//        $limits = $offsets.','.$limit;
        $res = $UserIntegralService->getMyUserIntegral($where,$limit,$offsets);
        if(!$res){
            $this->error($res['msg'],$res['data'],$res['code']);
        }
        $this->success($res['msg'],$res['data'],$res['code']);
    }

    /**
     * 商家财务统计
     */
    public function financial(){
        $UserMoneyLogService = new UserMoneyLogService();
        $userinfo = $this->userinfo;
        $uid = $userinfo['id'];
        $offset = $this->request->request('offset');
        $day = $this->request->request('day');

        //结束时间
        $endDate=$this->request->request('endDate',0);

        //周
        $week=$this->request->request('week',0);

        //月
        $monthDay=$this->request->request('monthday',0);

        //年
        $year=$this->request->request('year',0);

        $offsets = $offset?$offset:1;
        $number = $this->request->request('limit');
        $limit = $number?$number:10;
        $type = $this->request->request('type');
        $res=$UserMoneyLogService->getFinaData($type,$uid,$day,$endDate,$week,$monthDay,$year,$offsets,$limit);

        if(!$res){
            $this->error($res['msg'],$res['data'],$res['code']);
        }
        $this->success($res['msg'],$res['data'],$res['code']);

    }

}